package com.sy.service.impl;

import com.alibaba.fastjson.JSON;
import com.sy.dto.Params;
import com.sy.dto.PartnerDto;
import com.sy.dto.PartnersDto;
import com.sy.entity.Commit;
import com.sy.entity.DriSchool;
import com.sy.entity.Partner;
import com.sy.entity.Province;
import com.sy.service.PartnerService;
import com.sy.utils.ParamsUtils;
import com.sy.utils.RedisOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class PartnerServiceImpl extends BaseServiceImpl implements PartnerService {
    @Autowired
    private RedisOperations redisOperations;

    /**
     * 通过条件查询陪练信息
     *
     * @param params
     * @return
     */
    @Override
    public List<PartnersDto> getPartnerListByParams(Params params) {
        List<PartnersDto> partnerList;
        String key = "PartnerList" + ParamsUtils.add(params);
        if (redisOperations.exists(key)) {
            System.out.println("从Redis中直接获取...");
            partnerList = JSON.parseArray(redisOperations.get(key), PartnersDto.class);
        } else {
            String sort = "";
            System.out.println(params.toString());
            //获取排序方式
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            Integer start = (params.getPage() - 1) * 10;
            List<Province> provinces = new ArrayList<>();
//通过城市id获得该城市及区域内的所有区域集合
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            List<Integer> pIds = provinces.stream().map(province -> province.getPid()).collect(Collectors.toList());
            List<Partner> collect = partnerMapper.getPartnerByParams(pIds, params.getKeyWord(), sort, start);
            System.out.println(collect);
            partnerList = collect.stream().map(partner -> {
                PartnersDto partnersDto = new PartnersDto();
                if (!Objects.isNull(partner.getDriId())) {
                    DriSchool driSchoolById = driSchoolMapper.getDriSchoolById(partner.getDriId());
                    partnersDto.setDriSchoolId(driSchoolById.getDriSchoolId());
                    partnersDto.setDriName(driSchoolById.getDriName());
                }
                partnersDto.setPartner(partner);
                return partnersDto;
            }).collect(Collectors.toList());
            //存入redis中
            redisOperations.set(key, JSON.toJSONString(partnerList));
        }
        return partnerList;
    }

    /**
     * 通过条件查询当前条件下的陪练信息的总页码数
     *
     * @param params
     * @return
     */
    @Override
    public Integer getCountByParams(Params params) {
        String key = "PartnerCount" + ParamsUtils.add(params);
        Integer count;
//        if (redisOperations.exists(key)) {
//            System.out.println("从Redis中直接获取...");
//            count = JSON.parseObject(redisOperations.get(key), Integer.class);
//        } else {

            String sort = "";
            System.out.println(params.toString());
            //判断排序条件
            if (!Objects.isNull(params.getSortType())) {
                System.out.println(String.valueOf(params.getSortType()).trim());
                sort = (params.getSortType() == 1) ? "" : (params.getSortType() == 2 ? "price" : "count");
                System.out.println(sort);
            }
            List<Province> provinces = new ArrayList<>();
            //通过城市id获得该城市及区域内的所有区域集合
            if (!Objects.isNull(params.getPid())) {
                Province provinceByPid = provinceMapper.getProvinceByPid(params.getPid());
                provinces.add(provinceByPid);
                if (params.getPid() < 32) {
                    provinceMapper.getProvincesByProvinceId(params.getPid()).forEach(province -> provinces.add(province));
                }
            }
            List<Integer> pIds = provinces.stream().map(province -> province.getPid()).collect(Collectors.toList());
            Integer count1 = (int) partnerMapper.getCountByParams(pIds, params.getKeyWord(), sort);
        count = (count1 / 10);
        if(10*count<count1 & count>0){
            count=count+1;
        }
//            redisOperations.set(key, JSON.toJSONString(count));
//        }
        return count;
    }

    /**
     * 通过陪练id查询陪练的详细信息
     *
     * @param partnerId
     * @return
     */
    @Override
    public PartnerDto getPartnerDtoByPartnerId(Integer partnerId) {
//        String key = "partnerDto" + partnerId;
        PartnerDto partnerDto;
//        if (redisOperations.exists(key)) {
//            System.out.println("从Redis中直接获取...");
//            partnerDto = JSON.parseObject(redisOperations.get(key), PartnerDto.class);
//        } else {
            //通过陪练id获取陪练信息
            Partner partnerById = partnerMapper.getPartnerById(partnerId);
            //通过陪练获取陪练评论集合
            List<Commit> commitListByDrId = commitMapper.getCommitListByDrId(partnerId,3);
            //通过陪练id获取陪练所属驾校
            DriSchool driSchoolById = driSchoolMapper.getDriSchoolById(partnerById.getDriId());
            if(Objects.isNull(driSchoolById)){
                driSchoolById=new DriSchool();
                driSchoolById.setDriSchoolId(1);
            }
            partnerDto = new PartnerDto(driSchoolById.getDriSchoolId(), driSchoolById.getDriName(), commitListByDrId, partnerById);
//            redisOperations.set(key, JSON.toJSONString(partnerDto));
//        }
        return partnerDto;
    }
}
